package api.collection;
/**
 * 泛型
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class GenericDemo {
    public static void main(String[] args) {
        Collection<Point> c1=new ArrayList<>();
        c1.add(new Point(1,2));
        c1.add(new Point(3,4));
        c1.add(new Point(5,6));
        c1.add(new Point(7,8));
        c1.add(new Point(9,0));
        System.out.println(c1);
        Iterator<Point> p1= c1.iterator();
        while (p1.hasNext()){
            Point p= p1.next();
            System.out.println(p);
        }
        System.out.println("-----");
        for(Point p:c1){
            System.out.println(p);//会自动强转
        }
        System.out.println("-------------");
        Collection<String> c=new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        //c.add(333);//编译错误，123的类型违背了集合c所指向的泛型的实际类型String
        System.out.println(c);
        //迭代器所指的泛型应当与其遍历的集合的泛型一致
        Iterator<String> it=c.iterator();
        while (it.hasNext()){
            String str=it.next();
            System.out.println(str);
        }
        System.out.println("-----------");
        for (String str:c){
            System.out.println(str);
        }
    }
}
